clear
kk=load_win_keyboard;

ListenChar(0);% release keyboard from other users
uimenufcn(0,'WindowCommandWindow');
KbQueueCreate;

KbQueueStart;
if Eyelink2('Initialize', 'PsychEyelinkDispatchCallback')
    InitializeDummy=questdlg_offscreen('Eyelink is not properly connected. Initialize in Dummy mode?', ...
        'Eyelink is not connected!', ...
        'Yes','Quit','Yes');
    if strcmp(InitializeDummy,'Yes')
        InitializeDummy=true;
        if Eyelink2('InitializeDummy', 'PsychEyelinkDispatchCallback')
            fprintf('InitializeDummy failed too\n')
            return;
        end
    else
        fprintf('cancled by user\n')
        return;
    end
else
    InitializeDummy=false;
end
el = EyelinkInitDefaults;
    %el.msgfont='Tahoma';
    el.msgfontsize=15;%12
    %el.imgtitlefont='Tahoma';
    el.imgtitlefontsize=12;
    PsychEyelinkDispatchCallback(el); % need to update el on the host

KbQueueCreate;

KbQueueStart;
warning('off', 'PTB3:KbQueueBusy')
ListenChar(2);% BLOCK keyboard from MATLAB
warning('on', 'PTB3:KbQueueBusy')

keypress=0;
keycode=0;
lastkeycode=0;
KbQueueFlush();
kk=load_win_keyboard;
c_x=760;
c_y=420;
abs_start_time=round(GetSecs*1000);
EL1k_start_time = round(Eyelink2('TrackerTime')*1000);
Eyelink2('StartRecording'); % messages must be AFTER StartRecording and require SPRINTF to convert floats to strings

try
    while keycode~=27
        [keypress, keycodes, keytimes]= Kb_bcoeMS(abs_start_time);
        if keypress
            if keycodes(1)~=lastkeycode
                lastkeycode=keycodes(1);
                keycode=keycodes(1);
                keytime=keytimes(1);
                ttext=sprintf('T:%d C:%d (%s)',keytime, keycode,kk{keycode});
                fprintf('%s\n',ttext)
                Eyelink2('Command','clear_screen 0');
                Eyelink2('Command','draw_text %d %d %d %s ',round(c_x),round(c_y),8,ttext);
                
            end
        else
            [samples, eventsIN] = Eyelink('GetQueuedData');
            if ~isempty(eventsIN),
                sidewinder=eventsIN(:,eventsIN(2,:)==25);
                if any(sidewinder)
                    keyCodes=sidewinder(3,:);
                    keyTimes=sidewinder(1,:)-EL1k_start_time;
                    %[keyCode, loc,loc2]=intersect([SW.cell{:}],keyCodes);
                    keyCode=keyCodes(1);
                    if any(keyCode) && length(keyCode)==1 % only except one key press per 2ms
                        effector='sidewinder';
                        %keyTime=keyTimes(loc2);
                        keyTime=keyTimes(1);
                        ttext=sprintf('keyCode=%2d:  Time=%10d (%s)',keyCode,keyTime,effector);% requires SPRINTF
                        Eyelink2('Command','clear_screen 0');
                        Eyelink2('Command','draw_text %d %d %d %s ',round(c_x),round(c_y),8,ttext);
                    else
                        beep;
                    end
                end %if any(sidewinder)
            end% ~isempty(eventsIN)
        end % ~keypress        
    end
catch badness
    badness.message
    badness.stack.name
    badness.stack.line
end
ListenChar(0);% release keyboard from other users
KbQueueStop;
KbQueueRelease;
Eyelink2('StopRecording'); % messages must be AFTER StartRecording and require SPRINTF to convert floats to strings
Eyelink2('Shutdown');
fprintf(' !!!FIN!!!\n\n')